<?php

namespace App\Listeners;

use App\Events\SendSms;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Overtrue\EasySms\EasySms;
use Str;
use Illuminate\Support\Facades\Cache;

class SendSmsListener implements ShouldQueue
{
    /**
     * Create the event listener.
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     * @param \App\Events\SendSms $event
     * @return void
     */
    public function handle(SendSms $event)
    {
        $config = config('easysms');

        $easySms = new EasySms($config);

        $code = mt_rand(1000, 9999);

        Cache::put('phone_code_' . $event->phone, $code, now()->addMinutes(15));

        $easySms->send($event->phone, [
            'template' => config('easysms.gateways.juhe.templates.check_phone'),
            'data'     => [
                'code' => $code,
            ],
        ]);
    }
}
